using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.IO;
using System.Reflection;

using _videoConference.BAL;

namespace _videoConference
{
    /// <summary>
    /// Interaction logic for Zip_Management.xaml
    /// </summary>

    public partial class ZipManagement: System.Windows.Controls.UserControl
    {
        Label lzipname, lmodname, ldatetime;
        Label CaptionZipName = new Label();
        Label CaptionModuleName = new Label();
        Label CaptionDateTime = new Label();
        Button btnDelete, btnCancel;
        DataSet ds = new DataSet();
        IAddPage ia = new IAddPage();        

        public ZipManagement()
        {
            InitializeComponent();
            ZipInk.EditingMode = InkCanvasEditingMode.None;

            CaptionZipName.Foreground = Brushes.White;
            CaptionModuleName.Foreground = Brushes.White;
            CaptionDateTime.Foreground = Brushes.White;

            CaptionZipName.Content = "File Name";
            CaptionModuleName.Content = "Module Name";
            CaptionDateTime.Content = "Date Time";

            CaptionZipName.FontWeight = FontWeights.Bold;
            CaptionModuleName.FontWeight = FontWeights.Bold;
            CaptionDateTime.FontWeight = FontWeights.Bold;

            CaptionZipName.FontSize = 12.0;
            CaptionModuleName.FontSize = 12.0;
            CaptionDateTime.FontSize = 12.0;

            CaptionZipName.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionModuleName.SetValue(InkCanvas.TopProperty, 10.0);
            CaptionDateTime.SetValue(InkCanvas.TopProperty, 10.0);

            CaptionZipName.SetValue(InkCanvas.LeftProperty, 10.0);
            CaptionModuleName.SetValue(InkCanvas.LeftProperty, 150.0);
            CaptionDateTime.SetValue(InkCanvas.LeftProperty, 260.0);

            ZipInk.Children.Add(CaptionZipName);
            ZipInk.Children.Add(CaptionModuleName);
            ZipInk.Children.Add(CaptionDateTime);

            LinearGradientBrush l;
            GradientStop gs1;
            GradientStop gs2;

            l = new LinearGradientBrush();

            Point p1 = new Point(0.0, 0.5);
            Point p2 = new Point(1.0, 0.5);

            l.StartPoint = p1;
            l.EndPoint = p2;

            gs2 = new GradientStop(System.Windows.Media.Color.FromRgb(89, 89, 89), 0.11428571428571427);
            gs1 = new GradientStop(System.Windows.Media.Color.FromRgb(115, 115, 115), 0.51428571428571427);

            System.Windows.Media.RotateTransform RT;
            RT = new RotateTransform(90.0);

            l.Transform = RT;
            l.GradientStops.Add(gs1);
            l.GradientStops.Add(gs2);

            ds = ia.getzipinfo();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                lzipname = new Label();
                lzipname.Foreground = Brushes.White;
                lmodname = new Label();
                lmodname.Foreground = Brushes.White;
                ldatetime = new Label();
                ldatetime.Foreground = Brushes.White;               

                btnDelete = new Button();
                btnDelete.Content = "Delete";
                btnDelete.Width = 80.0;
                btnDelete.Background = l;
                btnDelete.Foreground = Brushes.White;
                btnDelete.Click += new RoutedEventHandler(btnDelete_Click);

                lzipname.Content = ds.Tables[0].Rows[i][2].ToString();
                lzipname.Tag = ds.Tables[0].Rows[i][0].ToString();
                lzipname.FontSize = 14.0;

                lmodname.Content = ds.Tables[0].Rows[i][1].ToString();
                lmodname.Tag = ds.Tables[0].Rows[i][0].ToString();
                lmodname.FontSize = 14.0;

                ldatetime.Content = ds.Tables[0].Rows[i][4].ToString();
                ldatetime.Tag = ds.Tables[0].Rows[i][0].ToString();
                ldatetime.FontSize = 14.0;

                btnDelete.Tag = ds.Tables[0].Rows[i][0].ToString();

                lzipname.SetValue(InkCanvas.LeftProperty, 10.0);
                lmodname.SetValue(InkCanvas.LeftProperty, 150.0);
                ldatetime.SetValue(InkCanvas.LeftProperty, 260.0);
                btnDelete.SetValue(InkCanvas.LeftProperty,450.0);

                lzipname.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                lmodname.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                ldatetime.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);
                btnDelete.SetValue(InkCanvas.TopProperty, (i + 1) * 31.0);

                ZipInk.Children.Add(lzipname);
                ZipInk.Children.Add(lmodname);
                ZipInk.Children.Add(ldatetime);
                ZipInk.Children.Add(btnDelete);
            }
           
            btnCancel = new Button();
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            btnCancel.Content = "Cancel";
            btnCancel.Width = 80.0;
            btnCancel.Height = 25.0;
            btnCancel.SetValue(InkCanvas.LeftProperty, 280.0);
            btnCancel.SetValue(InkCanvas.TopProperty, double.Parse(((Button)ZipInk.Children[ZipInk.Children.Count - 1]).GetValue(InkCanvas.TopProperty).ToString()) + 40.0);
            ZipInk.Children.Add(btnCancel);            

            btnCancel.Background = l;
            btnCancel.Foreground = Brushes.White;
                    
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
            ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
            ((InkCanvas)this.Parent).Children.Remove(this);
        }

        void btnDelete_Click(object sender, RoutedEventArgs e)
        {            
           MessageBoxResult r= MessageBox.Show("Are you sure that you want to delete the uploaded zip file?", "File Delete", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No, MessageBoxOptions.DefaultDesktopOnly);
           try
           {
               if (r == MessageBoxResult.Yes)
               {
                   string mid = "";
                   for (int i = 0; i < ZipInk.Children.Count; i++)
                   {
                       if (ZipInk.Children[i].GetValue(InkCanvas.LeftProperty).ToString() == "260" && ZipInk.Children[i].GetValue(InkCanvas.TopProperty).ToString()!="10")
                       {
                           if (((Label)ZipInk.Children[i]).Tag.ToString() == ((Button)sender).Tag.ToString())
                           {
                               mid = ((Button)sender).Tag.ToString();

                               DataSet dmod=ia.getzipmodule(mid);

                               if (dmod.Tables.Count > 0 && dmod.Tables[0].Rows.Count > 0)
                               {
                                   for (int k = 0; k < dmod.Tables[0].Rows.Count; k++)
                                   {
                                       Assembly ass = Assembly.GetAssembly(typeof(ZipManagement));
                                       string filezip = ass.Location.Replace("1videoConference.exe", @"Zip Files");
                                       string pathview = ass.Location.Replace("1videoConference.exe", @"Presentation Layer");
                                       string pathdal = ass.Location.Replace("1videoConference.exe", @"Data Layer");
                                       string pathbal = ass.Location.Replace("1videoConference.exe", @"Business Layer");

                                       if (File.Exists(filezip + "\\" + dmod.Tables[0].Rows[k][0].ToString() + ".zip"))
                                       {
                                           File.Delete(filezip + "\\" + dmod.Tables[0].Rows[k][0].ToString() + ".zip");
                                       }
                                       if (Directory.Exists(pathdal + "\\" + dmod.Tables[0].Rows[k][0].ToString()))
                                       {
                                           Directory.Delete(pathdal + "\\" + dmod.Tables[0].Rows[k][0].ToString(), true);
                                       }
                                       if (Directory.Exists(pathbal + "\\" + dmod.Tables[0].Rows[k][0].ToString()))
                                       {
                                           Directory.Delete(pathbal + "\\" + dmod.Tables[0].Rows[k][0].ToString(), true);
                                       }
                                       if (Directory.Exists(pathview + "\\" + dmod.Tables[0].Rows[k][0].ToString()))
                                       {                                           
                                           Directory.Delete(pathview + "\\" + dmod.Tables[0].Rows[k][0].ToString(),true);
                                       }                                      
                                       UserInfo.fuc.svcDeleteFolder(dmod.Tables[0].Rows[k][0].ToString());
                                   }
                               }

                               bool bool1 = ia.delzipinfo(mid);
                               if (bool1)
                               {
                                   int j = 0;
                                   while (j < ZipInk.Children.Count)
                                   {
                                       try
                                       {
                                           if (ZipInk.Children[j].GetType() == typeof(Label) && ((Label)ZipInk.Children[j]).Tag != null && ((Label)ZipInk.Children[j]).Tag.ToString() == ((Button)sender).Tag.ToString())
                                           {
                                               ZipInk.Children.Remove(ZipInk.Children[j]);
                                           }
                                           else if (ZipInk.Children[j].GetType() == typeof(Button) && ((Button)ZipInk.Children[j]).Tag != null && ((Button)ZipInk.Children[j]).Tag.ToString() == ((Button)sender).Tag.ToString())
                                           {
                                               ZipInk.Children.Remove(ZipInk.Children[j]);
                                           }
                                           else
                                           {
                                               j++;
                                           }
                                       }
                                       catch (Exception exp)
                                       {
                                           //MessageBox.Show(exp.Message);
                                           exp.Data.Add("My Key", "1videoConference--:--ManageMeeting.xaml.cs--:--btnDelete_Click()--:--");
                                           clsUtility.LogException = exp;
                                           break;
                                       }
                                   }
                               }
                               ((Canvas)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("cnvMain")).IsEnabled = true;
                               ((Menu)((Grid)((InkCanvas)((InkCanvas)this.Parent).Parent).Parent).FindName("mnu")).IsEnabled = true;
                               ((InkCanvas)this.Parent).Children.Remove(this);
                           }
                       }
                   }                  
               }
           }
           catch (Exception exp)
           {
               exp.Data.Add("My Key", "1videoConference--:--ZipManagement.xaml.cs--:--btnDelete_Click()1--:--");
               clsUtility.LogException = exp;
               //MessageBox.Show(exp.Message);

           }
        }
    }
}